using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
/*
songzuheng 
时间 : 10/13/2017 5:57:53 PM
描述 : 有bug可以找我改啊
*/
public class SerializableDictionaryEditor : EditorWindow
{
	public Transform PrefabObj;  
	public string TagName="Locator";  
  
	private GameObject[] objs;

	private SerializableDictionary<object,object> mdic;
	//设置插件在菜单栏的位置   和快捷键  
	[MenuItem("Window/编辑字典")]  
	//实例化窗体  
	static void Init()
	{
		bool isHave = false;
		GameObject go = Selection.activeGameObject;
		MonoBehaviour[] ms = go.GetComponents<MonoBehaviour>();
		foreach (var v in ms)
		{
			Type  type = v.GetType();
			FieldInfo[] ts = type.GetFields();
			foreach (var tif in ts)
			{
				Type FieldType = tif.FieldType;
				if (FieldType.BaseType.Name.Contains("SerializableDictionary"))
				{
					isHave = true;
				}
			}
		}
		if (isHave)
		{
			SerializableDictionaryEditor window =
				(SerializableDictionaryEditor) EditorWindow.GetWindow(typeof(SerializableDictionaryEditor));
			window.Show();
		}
		else
		{
			Debug.LogError("只有当物体脚本有字典是才可用");
		}
	}

	public void OnEnable()
	{
		GameObject go = Selection.activeGameObject;
		MonoBehaviour[] ms = go.GetComponents<MonoBehaviour>();
		
	}

	public void OnDisable()  
	{  
		
	}  
	//用GUI画出窗体的空间布局  
	void OnGUI()  
	{  
		GUILayout.BeginHorizontal();  
		GUILayout.Label("预制体", EditorStyles.boldLabel);  
		PrefabObj = EditorGUILayout.ObjectField(PrefabObj, typeof(Transform)) as Transform;  
		GUILayout.EndHorizontal();  
		GUILayout.BeginHorizontal();  
		GUILayout.Label("标签名称",EditorStyles.boldLabel);  
		TagName = EditorGUILayout.TextField(TagName,EditorStyles.boldLabel);  
		GUILayout.EndHorizontal();  
		if (GUILayout.Button("应用"))  
		{  
			fnDuplicate();  
		}  
	}  
	/// <summary>  
	/// 便利Tag下物体并实例化  
	/// </summary>  
	void fnDuplicate()  
	{  
		objs = GameObject.FindGameObjectsWithTag(TagName);  
		foreach(GameObject obj in objs)  
		{  
			Instantiate(PrefabObj, obj.transform.position, obj.transform.rotation);  
		}  
	}  
}
